import allConst from "@/Json.js"
import uniIcon from '@/components/uni-icon/uni-icon.vue';
import uniCalendar from '@/components/uni-calendar/uni-calendar.vue';
import * as Api from '@/syApis/syAddOrder/syaddApis.js';
import goodModel from '@/components/ShowModl.vue';
import settingOption from "@/components/settingOption/settingOption.vue";
export default {
	data() {
		return {
			//单据编号
			BillNo: "",
			//单据日期
			BillDate: "",
			//制单人
			CreateName: "",
			//银行编号pick列表
			BankPicker: [],
			//银行编号当前索引
			BankIndex: 0,
			//银行编号源数组
			BankIndexList: [],
			//收款方式当前索引
			giveMoneyIndex: -1,
			//收款方式pick列表
			giveMoneyIndexList: [],
			//收款方式源数组
			PayForFs: [],
			//往来单位源数组
			ClientMessage: [],
			//往来单位pick数组
			picker1: [],
			//业务员
			OperatorList: [],
			//业务员pick数组
			picker2: [],
			TabCur: 0,
			list: [{
				name: '单据信息',
			}],
			BillType: "",
			BillName: "",
			//制单人
			CreateName: uni.getStorageSync("UserName"),
			//收款金额
			parForMoney: 0,
			VIPID: 0,
			//是否选中了商品
			isSelectGood: false,
			searchKey: "",
			//应付款单据列表
			OrderGoodsList: [],
			//多规格商品弹框
			isShowScanModel: false,
			//扫码返回的单据
			scanGoodList: [],
			//选中的单据
			NowSelectGoods: {},
			selectLookMore: -1,
			//Vip客户弹框
			isShowVipModel: false,
			//Vip用户列表
			VipUserList: [],
			//Vip编号
			VipCode: "",
			//当前选择的业务员条目
			SelectOptionsObj: {
				OperatorName: "",
				OperatorID: ""
			},
			//银行账号
			SelectBankObj: {
				BankName: "",
				BankID: ""
			},
			//支付方式
			SelectPayWayObj: {
				id: "",
				name: ""
			},
			//往来单位
			SelectClientObj: {
				ClientID: "",
				ClientName: ""
			},
			//Vip卡
			SelectVipObj: {
				MemberCode: "",
				MemberID: ""
			},
			//业务员搜索
			SearchOptionName: "",
			//银行账号搜索
			SearchBankName: "",
			//支付方式搜索
			SearchPayWayName: "",
			//模态框名称
			modalName: "",
			//是否修改单据
			isChange: false,
			//是否查看更多表头
			isLookHeaderMore: false,
			//往来单位搜索字段
			SearchClientName: "",
			//Vip搜索条件
			SearchVipCode: "",
			//查看更多提示
			TbHeaderMore: "查看更多",
			isShowTip: false,
			isChange: false,
			Common: "",
			TempOptionList: [],
			TempStoreList: [],
			isUserRoot: {},
			unLoadimg: '../../static/unUplad.png',
			//银行账号临时数组
			BankTempList: [],
			//收款方式
			PayTempForFs: [],
			//本地保存的设置数组
			cgSettingLocation: {},
			// 打印设置弹出框
			modalNames: null,
			//打印份数
			printNum: 1,
			//打印机编号
			printCode: 0,
			isBillCheckQx: false,
			Remark: "",
			isSearch: false,
			isHide: false
		}
	},
	components: {
		uniCalendar,
		uniIcon,
		goodModel,
		settingOption
	},
	filters: {
		onlyTwoFloat(value) {
			return Math.round(parseFloat(value * 100)) / 100
		}
	},
	onHide() {
		this.isHide = true;
	},
	onPageScroll(e) {
		if (e.scrollTop > 400) {
			this.isShowTip = true;
		} else {
			this.isShowTip = false;
		}
	},
	onShow() {
		// this.$eventHub.$off('fireData');
		this.$eventHub.$off('VipListen');
		this.$eventHub.$off('fires');
		this.$eventHub.$off('clientListen');
		this.$eventHub.$off('AddChangeBill');
		this.isHide = false;
	},
	computed: {
		// goodAllAmount() {
		// 	var Ammount = 0;
		// 	var that = this;
		// 	this.OrderGoodsList.forEach(item => {
		// 		Ammount += that.compuTaxMoney(item);
		// 	});
		// 	return Math.round(Ammount * 100) / 100;
		// },
		// OrderGoodsListLength() {
		// 	var AllCount = 0;
		// 	var that = this;
		// 	this.OrderGoodsList.forEach(item => {
		// 		AllCount += parseFloat(item.Qty);
		// 	});
		// 	return AllCount;
		// }
	},
	onLoad(options) {
		// var _this = this;
		// Api.registerReciver(function(barcode){
		// 	_this.searchKey = barcode;
		// 	_this.search();
		// });

		this.BillName = options.BillName;
		this.getBillTime();
		this.BillType = options.BillName == '收款单' ? '11' : options.BillName == '付款单' ? '12' : '18';
		this.GetPayModeMessage();
		this.GetBankMessage();
		this.GetOperatorMessage();
		let PDA_Module_Android = uni.getStorageSync("PDA_Module_Android"); //获取单据权限
		PDA_Module_Android = JSON.parse(PDA_Module_Android);
		
		var that = this;
		PDA_Module_Android.forEach((item, index) => {
			if (this.BillName == item.ModuleName) {
				that.isUserRoot = item;
			}
		});
		if (options.type == 1) {
			console.log(options.oldOrder);
			let oldOrder = JSON.parse(options.oldOrder);
			this.oldOrder = oldOrder;

			// if (this.BillType == 18) {
			this.BillID = oldOrder.PayID;
			this.BillNo = oldOrder.PayNo;
			// }
			//  else {
			// 	this.BillID = oldOrder.BillID;
			// 	this.BillNo = oldOrder.BillNo;
			// }

			this.parForMoney = 0;
			this.SelectClientObj.ClientName = oldOrder.ClientName;
			this.SelectClientObj.ClientID = oldOrder.ClientID;
			this.SelectBankObj.BankName = oldOrder.BankName;
			this.SelectBankObj.BankId = oldOrder.BankId;
			this.SelectBankObj.BankId = oldOrder.BankId;
			this.SelectPayWayObj.PayType = oldOrder.PayType;
			this.SelectPayWayObj.name = oldOrder.PayMode;
			this.SelectPayWayObj.id = oldOrder.PayType;
			this.SelectOptionsObj.OperatorID = oldOrder.EmpID;
			this.SelectOptionsObj.OperatorName = oldOrder.EName;
			this.parForMoney = oldOrder.Total;
			this.CreateName = oldOrder.OpenName == "" ? uni.getStorageSync("UserName") : oldOrder.OpenName;
			this.Remark = oldOrder.Remark;
			if (this.BillType != 18) {
				
				this.getBillDetail(oldOrder.PayID);
			} else {
				this.SelectVipObj.MemberCode = oldOrder.MemberCode;
				this.SelectVipObj.MemberID = oldOrder.VIPID;

			}
			this.isChange = true;

			console.log(this.isChange);
		} else {
			this.isChange = false;
		}
		uni.setNavigationBarTitle({
			title: options.BillName
		});
		let barCodeRelus = uni.getStorageSync("BarCode", "");
		if (barCodeRelus != "") {
			this.barCodeRelus = JSON.parse(barCodeRelus);;
		}
		let SystemSettingInfo = uni.getStorageSync("SystemSettingInfo", ""); //存储系统设置
		SystemSettingInfo = JSON.parse(SystemSettingInfo);
		SystemSettingInfo.forEach((item, index) => {
			if (item.SystemName == '单据是否直接审核') {
				if (item.Value == "1") {
					that.isBillCheckQx = true;
				}
				// console.log(item.Value);
			}

		});
		

	},
	onPageScroll(e) {
		if (e.scrollTop > 400) {
			this.isShowTip = true;
		} else {
			this.isShowTip = false;
		}
	},
	onUnload() {
		console.log("页面卸载");
		try {
			Api.unRegisterReciver();
		} catch (e) {
			//TODO handle the exception
		}

	},
	methods: {
		getBillTime() {
			let date = new Date();
			var month = date.getMonth() + 1;
			if (month < 10) {
				month = "0" + month
			}
			var day = date.getDate();
			if (day < 10) {
				day = "0" + day;
			}
			this.BillDate = date.getFullYear() + "-" + month + "-" + day;
		},
		//删除选择的商品
		deleteSelectGood(e) {
			let index = e;
			var _this = this;
			Api.showConfirm("是否删除此单据", function(state) {
				if (state) {
					_this.removeItem(index);
				} else {

				}
			})
		},
		//移除
		removeItem($index) {
			//删除操作存储未被删除的数据
			var newGoodList = [];
			this.OrderGoodsList.forEach((item, index) => {
				if (index != $index) {
					newGoodList.push(item);
				}
			});
			this.OrderGoodsList = newGoodList;
		},
		defaultSetting() {
			this.clearAll();
			/**获取默认设置**/
			let settingList = uni.getStorageSync("settingList", "");
			if (settingList != "") {
				settingList = JSON.parse(settingList);
				// console.log(this.settingList);
				settingList.forEach((item, index) => {
					//如果是调拨单则不赋值供应商或客户

					if (item.Default_Name == "业务员") {
						this.SelectOptionsObj.OperatorName = item.Default_Value;
						this.SelectOptionsObj.OperatorID = item.Default_ID;
					}
					// if (item.Default_Name == '仓库') {
					// 	this.SelectInStoreObj.StoreName = item.Default_Value;
					// 	this.SelectInStoreObj.StoreID = item.Default_ID;
					// 	this.SelectOutStoreObj.StoreID = item.Default_ID;
					// 	this.SelectOutStoreObj.StoreName = item.Default_Value;
					// }
				});
			}
		},
		DateChange(e) {
			this.BillDate = e.detail.value;
		},
		//返回顶部
		scrollTop() {
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 0
			});
		},
		filterStore(e) {
			let value = e.detail.value;
			this.TempStoreList = [];
			var that = this;
			this.StoreList.forEach((item, index) => {
				if (item.StoreName.indexOf(value) > -1) {
					that.TempStoreList.push(item);
				}
			});
			if (that.TempStoreList.length == 0) {
				Api.showTip("查无数据");
			}
		},
		filterOption(e) {
			let value = e.detail.value;
			this.TempOptionList = [];
			var that = this;
			this.OperatorList.forEach((item, index) => {
				if (item.OperatorName.indexOf(value) > -1) {
					that.TempOptionList.push(item);
				}
			});
			if (that.TempOptionList.length == 0) {
				Api.showTip("查无数据");
			}
		},
		filterBank(e) {
			let value = e.detail.value;
			this.BankTempList = [];
			var that = this;
			this.BankIndexList.forEach((item, index) => {
				if (item.BankName.indexOf(value) > -1) {
					that.BankTempList.push(item);
				}
			});
			if (that.BankTempList.length == 0) {
				Api.showTip("查无数据");
			}
		},
		filterPayFor(e) {
			let value = e.detail.value;
			this.PayTempForFs = [];
			var that = this;
			this.PayForFs.forEach((item, index) => {
				if (item.name.indexOf(value) > -1) {
					that.PayTempForFs.push(item);
				}
			});
			if (that.PayTempForFs.length == 0) {
				Api.showTip("查无数据");
			}
		},
		//获取单据明细
		getBillDetail(BillNo) {
			this.BillID = BillNo;
			let data = {
				BillID: BillNo
			};
			var that = this;
			Api.SelectBillPayDetail(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == '获取登录信息失败') {

					} else if (res.data.ErrorMsg != '') {
						Api.showTip(res.data.ErrorMsg);
					} else {
						res.data.ResultMsg.forEach((item, index) => {
							// item.DiscountPrice = that.DiscountPrice(item);
							// item.Amount = that.Amount(item);
							// if (res.data.ResultMsg[index].Goods_Image_Path1 == '' || typeof res.data.ResultMsg[index].Goods_Image_Path1 ==
							// 	'undefined') {
							// 	res.data.ResultMsg[index].Goods_Image_Path1 = that.unLoadimg;
							// } else {
							// 	res.data.ResultMsg[index].Goods_Image_Path1 = allConst.pictureUrl + res.data.ResultMsg[index].Goods_Image_Path1;
							// }
							res.data.ResultMsg[index].payCount = res.data.ResultMsg[index].PayTotal;
						})
						that.OrderGoodsList = res.data.ResultMsg;
						console.log(that.OrderGoodsList);
					}
				}
			})
		},
		// DiscountPrice(item) {
		// 	return parseFloat(item.Price) * (parseFloat(item.Discount) / 100) * (1 + parseFloat(
		// 		item.Tax) / 100);
		// },
		// //计算总价
		// Amount(item) {
		// 	return parseFloat(item.Price) * parseFloat(item.Qty) * (parseFloat(item.Discount) / 100) * (1 + parseFloat(
		// 		item.Tax) / 100);
		// 	// return  PFPrice;
		// },
		//通过条码扫描商品
		GetBarcodeGoodsMessageModel(barcode) {
			let Model = "";
			if (this.cgSettingLocation.BarLike[0].state) {
				Model = "%like"
			} else if (this.cgSettingLocation.BarLike[1].state) {
				Model = "like%"
			} else if (this.cgSettingLocation.BarLike[2].state) {
				Model = "like"
			} else {
				Model = "="
			}
			let data = {
				BillName: this.BillName,
				BarCode: barcode,
				Model: Model,
				StoreID: this.SelectStoreObj.StoreID
			};
			var that = this;
			Api.GetBarcodeGoodsMessageModel(data, function(res) {
				console.log(res.data.ResultMsg);
				// Api.showTip(res.data.ResultMsg);
				if (res.data.ResultMsg != '') {
					if (res.data.ResultMsg.length == 0) {
						Api.showTip("查无数据");
						that.isSearch = false;
						return;
					}
					res.data.ResultMsg.forEach((item, index) => {
						res.data.ResultMsg[index].Qty = 1;
						res.data.ResultMsg[index].Discount = 100;
						res.data.ResultMsg[index].Tax = 0;
						res.data.ResultMsg[index].Remark = "";
						res.data.ResultMsg[index].GoodsStorm = 0;
						res.data.ResultMsg[index].Price = res.data.ResultMsg[index].PFPrice;
						res.data.ResultMsg[index].barcode = res.data.ResultMsg[index].BarCode;
						res.data.ResultMsg[index].URate = res.data.ResultMsg[index].urate;
						res.data.ResultMsg[index].StoreID = that.SelectStoreObj.StoreID;
						res.data.ResultMsg[index].stockqty = Math.round(res.data.ResultMsg[index].stockqty * 100) / 100;
						res.data.ResultMsg[index].PFPrice = Math.round(res.data.ResultMsg[index].PFPrice * 100) / 100;
						res.data.ResultMsg[index].ProductionDate = "";
						res.data.ResultMsg[index].validityDate = "";
						res.data.ResultMsg[index].IsGift = 0;
					});

					that.scanGoodList = res.data.ResultMsg;
					that.modalName = "StoreScanGoodModal";
					that.searchKey = "";
				} else if (res.data.ResultMsg == "") {
					Api.showTip("查无数据");
					that.isSearch = false;
				} else if (res.data.ErrorMsg == "获取登录信息失败") {
					that.isSearch = false;
				} else {
					that.isSearch = false;
				}
			})
		},
		//计算折扣后的总金额
		compuTaxMoney(item) {
			let PFPrice = 0;
			PFPrice = parseFloat(item.Price) * parseFloat(item.Qty) * (parseFloat(item.Discount) / 100) * (1 + parseFloat(
				item.Tax) / 100);

			return PFPrice;
		},
		lookOrder() {
			// this.AddChangeBill();
			// if(this.BillType==2||this.BillType==3||this.BillType)
			if (this.isChange) {
				uni.redirectTo({
					url: "../selPurchasePayOrder/selPurchase?BillName=" + this.BillName
				})
			} else {
				uni.navigateTo({
					url: "../selPurchasePayOrder/selPurchase?BillName=" + this.BillName
				})
			}

		},
		AddChangeBill() {
			var _this = this;
			//当前选择的商品的临时列表
			// var NewGoodListTemp = this.OrderGoodsList;
			this.$eventHub.$on('AddChangeBill', function(data) {
				let oldOrder = JSON.parse(data);
				console.log(oldOrder);
				// let oldOrder = JSON.parse(options.oldOrder);
				_this.oldOrder = oldOrder;
				_this.BillID = oldOrder.PayID;
				_this.BillNo = oldOrder.PayNo;
				_this.parForMoney = oldOrder.Total;
				_this.SelectClientObj.ClientName = oldOrder.ClientName;
				_this.SelectClientObj.ClientID = oldOrder.ClientID;
				_this.SelectBankObj.BankName = oldOrder.BankName;
				_this.SelectBankObj.BankId = oldOrder.BankId;
				_this.SelectBankObj.BankName = oldOrder.BankName;
				_this.SelectPayWayObj.PayType = oldOrder.PayType;
				_this.SelectPayWayObj.name = oldOrder.PayMode;
				_this.SelectOptionsObj.OperatorID = oldOrder.EmpID;
				_this.Remark = oldOrder.Remark;
				_this.SelectOptionsObj.OperatorName = oldOrder.EName;
				if (_this.BillType != 18) {
					_this.getBillDetail(oldOrder.PayID);
				} else {
					_this.SelectVipObj.MemberCode = oldOrder.MemberCode;
					_this.SelectVipObj.MemberID = oldOrder.VIPID;

				}
				_this.isChange = true;
				_this.$eventHub.$off('AddChangeBill');
			});
		},
		//返回顶部
		scrollTop() {
			uni.pageScrollTo({
				scrollTop: 0
			});
		},
		//查看表头更多信息
		lookHeaderMore() {
			this.isLookHeaderMore = !this.isLookHeaderMore;
			if (this.isLookHeaderMore) {
				this.TbHeaderMore = "收起更多"
			} else {
				this.TbHeaderMore = "查看更多"
			}
		},
		//选择往来单位
		AddClientListen() {
			let isClient = 2
			uni.navigateTo({
				url: '../clientSelect/clientSelect?isClient=' + isClient + ""
			})
			var that = this;
			this.$eventHub.$on('clientListen', function(data) {
				that.SelectClientObj = JSON.parse(data);
				that.$eventHub.$off('clientListen');
			})
		},
		//选择会员卡
		AddVipListen() {

			uni.navigateTo({
				url: '../VipMemberSelect/VipMemberSelect'
			})
			var that = this;
			this.$eventHub.$on('VipListen', function(data) {
				that.SelectVipObj = JSON.parse(data);
				that.$eventHub.$off('VipListen');
			})
		},
		//修改还是新增单据
		isChangeOrder() {
			//如果新增
			if (this.isChange) {
				this.Change_Bill_Order();

			} else {
				this.GetNewOrderNo(true);
			}
		},
		//修改单据
		Change_Bill_Order() {
			let Save_M_Data_Json = [];
			if (this.OrderGoodsList.length == 0 && this.BillType != 18) {
				Api.showTip("请至少选择一件单据");
				return;
			}
			if (this.BillNo == "") {
				Api.showTip("请先获取单号");
				return;
			}
			// if (this.parForMoney <= 0) {
			// 	if (this.BillType == 18) {
			// 		Api.showTip("充值金额不能小于0");
			// 		return;
			// 	} else if (this.BillType == 12) {
			// 		Api.showTip("付款金额不能小于0");
			// 		return;
			// 	} else {
			// 		Api.showTip("收款金额不能小于0");
			// 		return;
			// 	}
			// }
			let parForMoney = 0;
			if (this.BillType != 18) {
				// let parForMoney = 0;
				this.OrderGoodsList.forEach((item, index) => {
					parForMoney += parseFloat(item.payCount);
				});
			} else {
				parForMoney = this.parForMoney;
				if (parseFloat(parForMoney) == 0) {
					Api.showTip("充值金额不能小于0");
					return;
				}
			}
			let CreateID = uni.getStorageSync("UserCode");
			let item = {
				PayNo: this.BillNo,
				PayTime: this.BillDate,
				PayMode: this.SelectPayWayObj.name,
				PayType: this.BillType,
				ClientID: this.SelectClientObj.ClientID,
				EmpID: this.SelectOptionsObj.OperatorID,
				Total: parForMoney + "",
				CreateID: CreateID,
				VIPID: this.SelectVipObj.MemberID + "",
				BankId: this.SelectBankObj.BankID,
				Remark: this.Remark
			};
			var Save_D_Data_Json = [];
			this.OrderGoodsList.forEach((item, index) => {
				var item = {
					BillID: item.BillID,
					Total: item.Total,
					PayTotal: item.payCount + ""
				}
				Save_D_Data_Json.push(item);
			});
			let data = {};
			if (this.BillType == 18) {
				data = {
					BillID: this.BillID,
					Save_M_Data_Json: JSON.stringify(item),
					Save_D_Data_Json: JSON.stringify(Save_D_Data_Json)
				};
			} else {
				data = {
					BillID: this.BillID,
					Save_M_Data_Json: JSON.stringify(item),
					Save_D_Data_Json: JSON.stringify(Save_D_Data_Json)
				};
			}
			var _this = this;
			Api.Change_Bill_Pay(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == '获取登录信息失败') {

					} else if (res.data.ErrorMsg != '') {
						Api.showTip(res.data.ErrorMsg);
					} else {
						Api.showTip("修改成功");
						if (_this.cgSettingLocation.ycDy[0].state) {
							_this.isPrintOrder(true, res.data.BillID);
						}
						if (_this.cgSettingLocation.ycDy[1].state) {
							_this.isPrintOrder(false, res.data.BillID);
						}
						//如果没有勾选打印
						if (!_this.cgSettingLocation.ycDy[0].state && !_this.cgSettingLocation.ycDy[1].state) {
							if (_this.cgSettingLocation.djSh[0].state) {
								_this.isBillCheck(true);
							}
							if (_this.cgSettingLocation.djSh[1].state) {
								_this.isBillCheck(false);
							}
						}
						_this.clearAll(true);
					}
				}
			})


		},
		search() {
			this.GetBarcodeGoodsMessageModel(this.searchKey);
		},
		//搜索银行账号
		searchBank() {
			if (this.SearchBankName != '') {
				let Filter = "BankName like '%" + this.SearchBankName + "%'";
				this.GetBankMessage(Filter);
			} else {
				this.GetBankMessage();
			}
		},

		//搜索转账方式
		searchPayWay() {
			if (this.SearchPayWayName != '') {
				let Filter = "name like '%" + this.SearchPayWayName + "%'";
				this.GetPayModeMessage(Filter);
			} else {
				this.GetPayModeMessage();
			}
		},
		//搜索业务员
		searchOption() {
			if (this.searchOption != '') {
				let Filter = "OperatorName like '%" + this.SearchOptionName + "%'";
				this.GetOperatorMessage(Filter);
			} else {
				this.GetOperatorMessage();
			}
		},
		//搜索往来单位
		searchClient() {
			if (this.searchOption != '') {
				let Filter = "ClientName like '%" + this.SearchClientName + "%'";
				this.GetClientMessage(Filter);
			} else {
				this.GetClientMessage();
			}
		},
		// 选择业务员
		selectOptions(index) {
			this.SelectOptionsObj = this.OperatorList[index];
			this.modalName = null;
		},
		// 选择银行账号
		selectBankName(index) {
			this.SelectBankObj = this.BankTempList[index];
			this.modalName = null;
		},
		// 选择往来单位
		selectClientName(index) {
			this.SelectClientObj = this.ClientMessage[index];
			this.modalName = null;
		},
		showModal(e) {
			this.modalName = e.currentTarget.dataset.target
		},
		hideModal(e) {
			this.modalName = null
		},
		// 		//查询Vip客户
		// 		getVip(Filter='') {
		// 			let data = {
		// 				 Filter
		// 			};
		// 			var that = this;
		// 			Api.Get_MemberMessage(data, function(res) {
		// 				if (res.state) {
		// 					if (res.data.ErrorMsg == '获取登录信息失败') {
		// 
		// 					} else if (res.data.ErrorMsg != '') {
		// 						Api.showTip(res.data.ErrorMsg);
		// 					} else {
		// 						that.VipUserList = res.data.ResultMsg;
		// 					}
		// 				}
		// 			});
		// 		},
		//选择Vip卡号
		choosesVipUser(index) {
			this.VipCode = this.VipUserList[index].MemberCode;
			this.VIPID = this.VipUserList[index].MemberID;
			this.isShowVipModel = false;
		},
		//添加商品
		addSelectGoods() {
			if (this.NowSelectGoods.payCount == 0) {
				Api.showTip("请输入本次结算额");
				return;
			}
			//判断商品是否存在 0否 1 是
			var flag = 0;
			var _this = this;
			// console.log(this.NowSelectGoods);
			this.OrderGoodsList.forEach((item, index) => {
				if (item.BillNo == _this.NowSelectGoods.BillNo) {
					_this.OrderGoodsList[index].payCount = _this.payCount;
					flag = 1;
				}
			});
			if (flag == 0) {
				this.OrderGoodsList.push(this.NowSelectGoods);
			}
			this.NowSelectGoods = {};
			this.isSelectGood = false;
		},
		//选择扫码商品
		choosesGoods(index) {
			this.NowSelectGoods = this.scanGoodList[index];
			console.log(this.NowSelectGoods);
			this.isShowScanModel = false;
			this.isSelectGood = true;
		},
		//扫码获取商品
		tel: function() {
			// Api.showTip("网页端暂不支持此功能");
			// return;
			this.current = -1;
			// 调起条码扫描
			var _this = this;
			uni.scanCode({
				scanType: 'barCode',
				success: function(res) {
					_this.GetPayBill(res.result);
					console.log('条码类型：' + res.scanType);
					console.log('条码内容：' + res.result);
				},
				fail(e) {
					console.log(e);
				}
			});
		},
		//扫码获取单据
		GetPayBill(Filter) {
			let data = {
				ClientID: this.ClientId,
				Filter: "BillNo=" + Filter
			}
			var self = this;
			Api.GetPayBill(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == '获取登录信息失败') {

					} else if (res.data.ErrorMsg != '') {
						Api.showTip(res.data.ErrorMsg);
					} else {
						res.data.ResultMsg.forEach((item, index) => {
							item.payCount = 1;
						});
						self.scanGoodList = res.data.ResultMsg;
						self.isShowScanModel = true;
					}
				} else {
					Api.showTip("服务器开小差了");
				}
			})
		},
		//按下事件
		clickNow(index) {
			console.log("按下");
			this.endTime = (new Date()).valueOf();
			var numTime = 0;
			var _this = this;
			this.touchTime = setInterval(function() {
				numTime++;
				if (numTime == 1) {
					clearInterval(_this.touchTime);
					Api.showConfirm("是否删除此商品", function(state) {
						if (state) {
							_this.removeItem(index);
						} else {

						}
					})
				}
			}, 1000);
		},

		//移除
		removeItem($index) {
			//删除操作存储未被删除的数据
			var newGoodList = [];
			this.OrderGoodsList.forEach((item, index) => {
				if (index != $index) {
					newGoodList.push(item);
				}
			});
			this.OrderGoodsList = newGoodList;
		},
		//抬起事件
		clickEnd(index) {
			clearInterval(this.touchTime);
			let nowTime = (new Date()).valueOf();
			if (nowTime - this.endTime <= 1000) {
				// this.changeInfo(index);
			}
		},
		//获取更多商品信息
		lookGoodsInfo(index) {
			if (this.selectLookMore == index) {
				this.selectLookMore = -1;
			} else {
				this.selectLookMore = index;
			}
		},
		gogoodlist() {
			if (this.BillNo == "") {
				Api.showTip("请先创建新单");
				return;
			};
			this.AddListen();
			var IsPay = this.BillType == 11 ? 0 : 1;
			if (this.SelectClientObj.ClientName == '') {
				Api.showTip("请先选择往来单位");
				return;
			}
			uni.navigateTo({
				url: './GetPayBill?ClientId=' + this.SelectClientObj.ClientID + "&IsPay=" + IsPay
			})
		},
		//获取转账方式
		GetPayModeMessage(Filter = '') {
			let data = {
				Filter
			};
			var that = this;
			Api.GetPayModeMessage(data, function(res) {
				console.log(res.data.ResultMsg);
				if (res.state) {
					// Api.showTip(res.data.ResultMsg);
					if (res.data.ResultMsg != '') {
						if (res.data.ResultMsg.length == 0) {
							Api.showTip("查无数据");
						}
						let responseData = res.data.ResultMsg;
						if (Filter == "") {
							that.PayForFs = responseData;
							that.PayTempForFs = responseData;
						} else {
							that.PayTempForFs = responseData;
						}

					} else if (res.data.ResultMsg == "") {
						Api.showTip("查无数据");
					} else if (res.data.ErrorMsg == "获取登录信息失败") {

					} else {

					}
				} else {
					Api.showTip("服务器开小差了");
				}
			})
		},
		//转账方式e
		selectPayWay(index) {
			this.SelectPayWayObj = this.PayTempForFs[index];
			this.modalName = null;
		},
		//查询往来单位
		GetClientMessage(Filter = '') {
			let data = {
				SelectAll: 1,
				Filter
			};
			var that = this;
			Api.GetClientMessage(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == "获取登录信息失败") {
						Api.showTip("登录超时");
					} else if (res.data.ErrorMsg != "") {
						Api.showTip(res.data.ErrorMsg);
					} else {
						that.ClientMessage = res.data.ResultMsg;
					}
				} else {

				}
			});
		},
		//点击获取单号
		GetNewOrderNo(isPull) {
			this.getBillTime();
			if (!isPull) {
				this.defaultSetting();
			} else {
				if (this.OrderGoodsList.length == 0 && this.BillType != 18) {
					Api.showTip("请至少选择一件单据");
					return;
				}
				if (this.BillNo == "") {
					Api.showTip("请先创建新单");
					return;
				}
				// if (this.parForMoney <= 0) {
				// 	if (this.BillType == 18) {
				// 		Api.showTip("充值金额不能小于0");
				// 		return;
				// 	} else if (this.BillType == 12) {
				// 		Api.showTip("付款金额不能小于0");
				// 		return;
				// 	} else {
				// 		Api.showTip("收款金额不能小于0");
				// 		return;
				// 	}
				// }
			}
			if (this.isUserRoot.IsBill_AddNew == "") {
				Api.showTip("无新增权限");
				return;
			}
			let data = {
				BillType: this.BillType,
				IsAdd: isPull ? 1 : 0,
				BillDate: this.BillDate
			};
			var that = this;
			Api.NewOrderNo(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == "获取登录信息失败") {
						Api.showTip("登录超时");
					} else if (res.data.ErrorMsg != "") {
						Api.showTip(res.data.ErrorMsg);
					} else {
						that.BillNo = res.data.ResultMsg.BillNo
						if (isPull) {
							that.AddBillOrder();
						}
					}
				} else {

				}
			});
		},
		//增加商品事件监听
		AddListen() {
			var _this = this;
			//当前选择的商品的临时列表
			var NewGoodListTemp = this.OrderGoodsList;
			this.$eventHub.$on('fires', function(data) {
				let GoodsItem = JSON.parse(data);
				if (!_this.IsInOrderList(GoodsItem).isIn) {
					NewGoodListTemp.push(GoodsItem);
				}
				this.OrderGoodsList = NewGoodListTemp;
				_this.$eventHub.$off('fires');
			});
		},
		//判断当前增加的商品是否存在
		IsInOrderList(item) {
			console.log(item);
			for (var i = 0; i < this.OrderGoodsList.length; i++) {
				if (this.OrderGoodsList[i].BillNo == item.BillNo) {
					item.isIn = true;
					this.OrderGoodsList[i].payCount = parseFloat(this.OrderGoodsList[i].payCount) + parseFloat(item.payCount);
					return item;
				}
			}
			item.isIn = false;
			return item;
		},
		//获取银行账号
		GetBankMessage(Filter = "") {
			var that = this;
			let data = {
				Filter
			};
			Api.GetBankMessage(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == "获取登录信息失败") {
						Api.showTip("登录超时");
					} else if (res.data.ErrorMsg != "") {
						Api.showTip(res.data.ErrorMsg);
					} else {
						console.log(res);
						let responseData = res.data.ResultMsg;
						if (Filter == "") {
							that.BankIndexList = responseData;
							that.BankTempList = responseData;
						} else {
							that.BankTempList = responseData;
						}

					}
				} else {
					console.log(res);
				}
			});
		},
		open() {
			this.$refs.calendar.open();
		},
		open1() {
			this.$refs.calendar1.open();
		},
		open2() {
			this.$refs.calendar2.open();
		},
		confirm(e) {
			this.BillDate = e.fulldate;
		},
		confirm1(e) {
			this.ProductionDate = e
		},
		confirm2(e) {
			this.ValidityDate = e
		},
		//获取业务员列表
		GetOperatorMessage(Filter = "") {
			let data = {
				Filter: Filter
			}
			var that = this;
			Api.GetOperatorMessage(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == "获取登录信息失败") {
						Api.showTip("登录超时");
					} else if (res.data.ErrorMsg != "") {
						Api.showTip(res.data.ErrorMsg);
					} else {
						let responseData = res.data.ResultMsg;
						if (Filter == '') {
							that.OperatorList = responseData;
							that.TempOptionList = responseData;
						} else {
							that.TempOptionList = responseData;
						}


					}
				} else {

				}
			});
		},
		//清空单据
		sureClear() {
			var that = this;
			uni.showModal({
				title: "是否清空单据",
				content: "清空后不可恢复,是否确认",
				success(res) {
					if (res.confirm) {
						that.clearAll();
					}
				}
			})
		},
		//清空单据
		clearAll(isBill=false) {
			
			this.BillNo = "";
			//当前选择的业务员条目
			this.OrderGoodsList = [];
			this.SelectOptionsObj = {
				OperatorID: "",
				OperatorName: ""
			};
			this.Remark = "";
			//当前选择的银行条目
			this.SelectBankObj = {
					BankName: "",
					BankID: ""
				},
				//当前选择的供应商或者客户条目
				this.SelectClientObj = {
					ClientName: "",
					CreateID: ""
				},
				//当前选择的转账方式
				this.SelectPayWayObj = {
					id: "",
					name: ""
				},
				//vip
				this.SelectVipObj = {
					MemberCode: "",
					MemberID: ""
				},
				//转账金额
				this.parForMoney = 0;
				if(isBill){
					
						this.GetNewOrderNo(false);
					
				}
			
				this.isChange = false;
		},
		//提交单据
		AddBillOrder() {
			let Save_M_Data_Json = [];
			let parForMoney = 0;
			if (this.BillType != 18) {
				// let parForMoney = 0;
				this.OrderGoodsList.forEach((item, index) => {
					parForMoney += parseFloat(item.payCount);
				});
			} else {
				parForMoney = this.parForMoney;
				if (parseFloat(parForMoney) == 0) {
					Api.showTip("充值金额不能小于0");
					return;
				}
			}
			console.log(this.parForMoney)
			let CreateID = uni.getStorageSync("UserCode");
			let item = {
				PayNo: this.BillNo,
				PayTime: this.BillDate,
				PayMode: this.SelectPayWayObj.name,
				PayType: this.BillType,
				ClientID: this.SelectClientObj.ClientID,
				EmpID: this.SelectOptionsObj.OperatorID,
				Total: parForMoney + "",
				CreateID: CreateID,
				VIPID: this.SelectVipObj.MemberID + "",
				BankId: this.SelectBankObj.BankID,
				Remark: this.Remark
			};
			var Save_D_Data_Json = [];
			this.OrderGoodsList.forEach((item, index) => {
				var item = {
					BillID: item.BillID,
					Total: item.Total,
					PayTotal: item.payCount + ""
				}
				Save_D_Data_Json.push(item);
			});
			let data = {};
			if (this.BillType == 18) {
				data = {
					Save_M_Data_Json: JSON.stringify(item),
					Save_D_Data_Json: JSON.stringify(Save_D_Data_Json)
				};
			} else {
				data = {
					Save_M_Data_Json: JSON.stringify(item),
					Save_D_Data_Json: JSON.stringify(Save_D_Data_Json)
				};
			}
			var self = this;
			Api.AddBillPay(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == '获取登录信息失败') {

					} else if (res.data.ErrorMsg != '') {
						Api.showTip(res.data.ErrorMsg);
					} else {
						Api.showTip("保存成功");
						setTimeout(function() {
							self.BillID = res.data.BillID;
							if (self.cgSettingLocation.ycDy[0].state) {
								self.isPrintOrder(true, res.data.BillID);
							}
							if (self.cgSettingLocation.ycDy[1].state) {
								self.isPrintOrder(false, res.data.BillID);
							}
							//如果没有勾选打印
							if (!self.cgSettingLocation.ycDy[0].state && !self.cgSettingLocation
								.ycDy[1].state) {
								if (!self.isBillCheckQx) {
									if (self.cgSettingLocation.djSh[0].state) {
										self.isBillCheck(true);
									}
									if (self.cgSettingLocation.djSh[1].state) {
										self.isBillCheck(false);
									}
								} else {
									self.isBillCheck(false);
								}
							}
						}, 500);

						self.clearAll(true);
					}
				}
			})
		},
		//打印份数监听
		getPrintNum(e) {
			this.printNum = e.detail.value
		},
		//打印机编号选择事件
		radioChange(e) {
			this.printCode = e.detail.value
		},
		//是否打印单据
		isPrintOrder(type, BillID = "") {
			var that = this;
			if (type) {
				Api.showConfirm("是否打印单据", function(result) {
					if (result) {
						that.PrintOrder(false, BillID);
					} else {
						setTimeout(function() {
							if (!that.isBillCheckQx) {
								if (that.cgSettingLocation.djSh[0].state) {
									that.isBillCheck(true);
								}
								if (that.cgSettingLocation.djSh[1].state) {
									that.isBillCheck(false);
								}
							} else {
								that.isBillCheck(false);
							}
						}, 1000);
					}
				})
			} else {
				that.PrintOrder(false, BillID);
			}
		},
		//是否审核
		isBillCheck(type) {
			var that = this;
			if (type) {
				Api.showConfirm("是否审核单据", function(result) {
					if (result) {
						that.toBillCheck();
					} else {

					}
				})
			} else {
				that.toBillCheck();
			}
		},
		toBillCheck(isInput, BillID = "") {
			var that = this;
			let data = {
				BillType: this.BillType,
				BillID: this.BillID,
				BillName: this.BillName
			};
			Api.Bill_Check(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == '获取登录信息失败') {
						Api.showTip("登录超时!");
					} else if (res.data.ErrorMsg != "") {
						Api.showTip(res.data.ErrorMsg);
					} else {
						// that.isBillCheckSuc[that.isBillCheck] = checkCountList[that.isBillCheck];
						// that.isBillCheck++;
						Api.showTip("审核成功");
					}
				} else {

				}
				// that.toBillCheck(checkCountList, index, countSize);
			})
		},
		PrintOrder(isInput = true, BillID = "") {
			let data = {};
			let Save_M_Data_Json = {};
			if (!isInput) {
				Save_M_Data_Json = {
					Order_Number: this.BillNo,
					BillName: this.BillName,
					Printer: this.CreateName,
					BillID: this.BillID
				};
			} else {
				this.modalNames = null;
				Save_M_Data_Json = {
					Order_Number: this.BillNo,
					BillName: this.BillName,
					Printer: this.CreateName,
					BillID: this.BillID,
					PrintNum: this.printNum + "",
					PrintPCNumber: this.printCode + ""
				};
			}
			data = {
				Save_M_Data_Json: JSON.stringify(Save_M_Data_Json)
			}
			this.printNum = 1;
			this.current = 0;
			this.printCode = 1;
			var _this = this;
			Api.Add_Android_Print(data, function(res) {
				if (res.state) {
					if (res.data.ErrorMsg == '获取登录信息失败') {
						Api.showTip("登录超时");
					} else if (res.data.ErrorMsg != "") {
						Api.showTip(res.data.ErrorMsg);
					} else {
						Api.showTip("已发送打印指令");
						if (!isInput) {
							setTimeout(function() {
								if (!_this.isBillCheckQx) {
									if (_this.cgSettingLocation.djSh[0].state) {
										_this.isBillCheck(true);
									}
									if (_this.cgSettingLocation.djSh[1].state) {
										_this.isBillCheck(false);
									}
								} else {
									_this.isBillCheck(false);
								}
							}, 1000);
						}
					}
				}
			})
		},
		ycDy() {
			if (this.isChange) {
				this.$refs.settingOption._data.modalPrintName = "printSetting";
			} else {
				Api.showTip("请先查询单据");
			}
		},
		// //单据审核
		djSh() {
			if (this.isChange) {
				this.isBillCheck();
			} else {
				Api.showTip("请先查询单据");
			}
		},
		onNavigationBarButtonTap(e) {
			const index = e.index;
			// console.log(index);
			if (index == 0) {
				this.modalName = 'DrawerModalR';
			}
		},
		changeSetting(e) {
			this.cgSettingLocation = e.detail;
		}
	}
}
